<?php


if (!defined('DOKU_INC'))define('DOKU_INC', realpath(dirname( __FILE__ ).'/../../').'/');
if (!defined('DOKU_PLUGIN'))define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
require_once (DOKU_PLUGIN.'syntax.php');


class syntax_plugin_jalbum_jalbumbadge extends DokuWiki_Syntax_Plugin
{
    /**
     * default parameters of the jalbum tag
     */
    var $dflt = array (
    'user'=>'jaloma',
    'layout'=>'5',
    'header'=>'My JAlbums',
    'width'=>'off',
    'height'=>'off',
    'simple_footer_lnks'=>'1',
    );


    var $layoutmapping = array (
    "Light"=>1,
    "Dark"=>2,
    "Blue"=>3,
    "Pink"=>4,
    "Green"=>5,
    "Brown"=>6,
    "light"=>1,
    "dark"=>2,
    "blue"=>3,
    "pink"=>4,
    "green"=>5,
    "brown"=>6,
    );
	
    /**
     * return some info
     */
    function getInfo()
    {
        return array (
        'author'=>'Juergen A.Lamers',
        'email'=>'jaloma.ac@googlemail.com',
        'date'=>@file_get_contents(DOKU_PLUGIN.'jalbum/VERSION'),
        'name'=>'jalbumbadge',
        'desc'=>'Einfacher Zugriff auf das Badge von JAlbum-Net',
        'url'=>'http://wiki.dokuwiki.org/plugin:jalbum'
        );
    }

    /**
     * What kind of syntax are we?
     */
    function getType()
    {
        return 'substition';
    }

    function getSort()
    {
        return 999;
    }

    function connectTo($mode)
    {
        $this->Lexer->addSpecialPattern('<jalbumbadge.*?/>', $mode, 'plugin_jalbum_jalbumbadge');
    }

    function matchLength()
    {
        return strlen('<jalbumbadge');
    }

    /**
     * Handle the match
     */
    function handle($match, $state, $pos, & $handler)
    {
        $match = html_entity_decode(substr($match, $this->matchLength(), -2));
        $thumb = $this->_extract_params($match);
        return array ($thumb);
    }

    /**
     * Create output
     * <script type="text/javascript" charset="utf-8">
     _ja_badge_header = "My Jalbums";
     _ja_badge_look = 5;
     
     _ja_badge_width = 200;
     _ja_badge_simple_footer_lnks = 1;
     
     </script>
     <script src="http://jalbum.net/badge/load.js?u=jaloma&v=1" type="text/javascript" charset="utf-8"></script>
     */
    function render($mode, & $renderer, $data)
    {
        global $conf;
        if ($mode == 'xhtml')
        {
            list ($param) = $data;
            if ( isset ($param['user']))
            {
                $title = $param['header'];
                $user = $param['user'];
                $layout = $param['layout'];
				if (array_key_exists($layout,$this->layoutmapping)) {
					$layout = $this->layoutmapping[$layout];
				}
                $width = $param['width'];
                $height = $param['height'];
//                echo $layout."<br/>";
                $simple_footer_lnks = $param['simple_footer_lnks'];
//				$txt .= '<p>';
                $txt .= '<p style="clear:none;">';
                //<![CDATA[Inhalt]]]]><![CDATA[>Inhalt]]>
                $txt .= '<script type="text/javascript" charset="utf-8">'."\n";
                if ( isset ($height) && $height != 'off')
                {
                    $txt .= '_ja_badge_height = '.$height.";\n";
                }
                //'// <![CDATA['."\n".

                if ( isset ($width) && $width != 'off')
                {
                    $txt .= '_ja_badge_width = '.$width.";\n";
                }
                $txt .= '_ja_badge_simple_footer_lnks = '.$simple_footer_lnks.";\n";
                $txt .= '_ja_badge_look = '.$layout.';'."\n";
                $txt .= '_ja_badge_header = "'.$title.'";'."\n";
                $txt .= ''.
                //'//]]>'."\n".
                '</script>'."\n".
                //.'
                '<script src="http://jalbum.net/badge/load.js?u='.$user.'&amp;v=1" type="text/javascript" charset="utf-8"></script>'."\n";
				
                $txt .= '</p>';
//				$txt .= '<h3> </h3>'; 
//				$txt .= '</p>';
                $renderer->doc .= $txt;
            } else
            {
                return false;
            }
        }
        return true;
    }

    /**
     * extract parameters for jalbum from the parameter string
     *
     * @param   string    $str_params   string of key="value" or key='value' pairs
     * @return  array                   associative array of parameters key=>value
     */
    function _extract_params($str_params)
    {
        $param = array ();
        preg_match_all('/(\w*)="(.*?)"/us', $str_params, $param, PREG_SET_ORDER);
        if (sizeof($param) == 0)
        {
            preg_match_all("/(\w*)='(.*?)'/us", $str_params, $param, PREG_SET_ORDER);
        }
        // parse match for instructions, break into key value pairs
        $thumb = $this->dflt;
        foreach ($param as $kvpair)
        {
            list ($match, $key, $val) = $kvpair;
            //      $key = strtolower($key);
            if ( isset ($thumb[$key]))$thumb[$key] = $val;
        }
        return $thumb;
    }

} // class

?>
